package cn.akwangl.springbootdev.exception;

import cn.akwangl.springbootdev.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class CommonExceptionHandler {

    /**
     * 拦截Exception类的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        Result result = new Result();
        if (e instanceof ServiceException) {
            ServiceException serviceException = (ServiceException) e;
            result.setCode(serviceException.getCode());
            result.setMsg(serviceException.getMsg());
            return result;
        }
        e.printStackTrace();

        return Result.error();
    }

}
